package com.hzdl.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.*;
import com.hzdl.car.mapper.CarMapper;
import com.hzdl.car.mapper.RentMapper;
import com.hzdl.car.service.CarService;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.service.RentService;
import com.hzdl.car.vo.CarVo;
import com.hzdl.car.vo.RentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class RentServiceImpl implements RentService {
    @Resource
    private RentMapper rentMapper;
    @Resource
    private CarMapper carMapper;
    @Override
    public int addRent(RentVo rentVo) {
        CarVo carVo=new CarVo();
        carVo.setIsrenting(1);
        carVo.setCarNumber(rentVo.getCarNumber());
        carMapper.carIsRent(carVo);
        return rentMapper.addRent(rentVo);
    }

    @Override
    public int delRent(RentVo rentVo) {
        Rent rent=rentMapper.findCarNById(rentVo);
        CarVo carVo=new CarVo();
        carVo.setIsrenting(0);
        carVo.setCarNumber(rent.getCarNumber());
        carMapper.carIsRent(carVo);
        return rentMapper.delRent(rentVo);
    }

    @Override
    public int updateRent(RentVo rentVo) {
        return rentMapper.updateRent(rentVo);
    }

    @Override
    public PageInfo<Rent> findRent(RentVo rentVo) {
        PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        List<Rent> list = rentMapper.findRent(rentVo);
        PageInfo<Rent> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Rent findCarNById(RentVo rentVo) {
        return rentMapper.findCarNById(rentVo);
    }

    @Override
    public Map<String, Object> findPriceByYear(Integer year) {
        Map<String, Object> map=new HashMap<>();
        List<BaseStat> list=rentMapper.findPriceByYear(year);
        List<String> name=new ArrayList<>();
        List<Object> value=new ArrayList<>();
        for(BaseStat i:list){
            name.add(i.getName());
            value.add(i.getValue());
        }
        map.put("name",name);
        map.put("value",value);
        map.put("year",year);
        return map;
    }

    @Override
    public List<String> companyPriceByYear(Integer year) {
        return rentMapper.companyPriceByYear(year);
    }

    @Override
    public RentExcel findRentExcel(String rentId) {
        return rentMapper.findRentExcel(rentId);
    }
}
